package com.lc.qianrenshiye.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.qianrenshiye.BaseActivity;
import com.lc.qianrenshiye.BaseApplication;
import com.lc.qianrenshiye.R;
import com.lc.qianrenshiye.adapter.OrderAdapter;
import com.lc.qianrenshiye.conn.OrderListPost;
import com.lc.qianrenshiye.dialog.DateTimePickDialogUtil;
import com.lc.qianrenshiye.uitls.MyAnimationUitls;
import com.lc.qianrenshiye.uitls.MyToast;
import com.lc.qianrenshiye.uitls.TimeUtil;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/11/19.
 */
public class OrderActivity extends BaseActivity implements View.OnClickListener, OrderAdapter.PayOrContinue {

    @BoundView(R.id.order_lv_bottom_tv) private ListView order_lv_bottom_tv;
    @BoundView(R.id.order_lv) private PullToRefreshListView pullToRefreshListView;
    private ListView order_lv;
    @BoundView(R.id.order_lv_top_tips_rl) private RelativeLayout order_lv_top_tips_rl;
    @BoundView(R.id.order_search_tv) private TextView order_search_tv;
    @BoundView(R.id.order_time_end_tv) private TextView order_time_end_tv;
    @BoundView(R.id.order_time_start_tv) private TextView order_time_start_tv;

    @BoundView(R.id.order_tab_not_pay_rb) private RadioButton order_tab_not_pay_rb;
    @BoundView(R.id.order_tab_payed_rb) private RadioButton order_tab_payed_rb;
    @BoundView(R.id.order_tab_all_rb) private RadioButton order_tab_all_rb;
    @BoundView(R.id.order_tab_rg) private RadioGroup order_tab_rg;

    private List<OrderListPost.OrderEntity> lists = new ArrayList<>();
    private OrderAdapter adapter;

    private boolean isRefresh;

    private OrderListPost orderListPost = new OrderListPost(new AsyCallBack<OrderListPost.OrderListEntity>() {
        @Override
        public void onSuccess(String toast, int type, OrderListPost.OrderListEntity orderListEntity) throws Exception {

            lists.clear();

            if (type == 1) {

                MyAnimationUitls.toVisibleAnim(order_lv);

            } else if (type==2) {

                isRefresh = true;

                order_tab_all_rb.setChecked(true);

            }

            lists.addAll(orderListEntity.orderEntityList);

            adapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {

            pullToRefreshListView.onPullUpRefreshComplete();
            pullToRefreshListView.onPullDownRefreshComplete();

            setLastUpdateTime();
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);

        setBackTrue();

        setTitleName(getString(R.string.order_title_text));

        View headView = BoundViewHelper.boundView(this, ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) LayoutInflater.from(this).inflate(R.layout.activity_order_head, null)));

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        order_time_start_tv.setHint(simpleDateFormat.format(new Date()).toString());
        order_time_end_tv.setHint(simpleDateFormat.format(new Date()).toString());

        pullToRefreshListView.setScrollLoadEnabled(false);
        pullToRefreshListView.setPullLoadEnabled(true);
        pullToRefreshListView.setPullRefreshEnabled(true);

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                order_time_start_tv.setText("");
                order_time_end_tv.setText("");

                orderListPost.time1 = "";
                orderListPost.time2 = "";
                orderListPost.pay = "";

                orderListPost.execute(OrderActivity.this, false, 2);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                pullToRefreshListView.onPullUpRefreshComplete();

            }
        });

        order_lv = pullToRefreshListView.getRefreshableView();

        order_lv.setVisibility(View.GONE);

        order_lv.addHeaderView(headView, null, false);

        order_lv.setDivider(null);

        order_lv.setVerticalScrollBarEnabled(false);

        order_lv.setAdapter(adapter = new OrderAdapter(context, lists, this));

        order_tab_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId) {

                    case R.id.order_tab_all_rb:

                        if (isRefresh) {

                            Log.e("orderActivity", "isRefresh" + isRefresh);

                            isRefresh = false;

                            return;
                        }

                        orderListPost.pay = "";

                        orderListPost.execute(OrderActivity.this);

                        break;

                    case R.id.order_tab_payed_rb:

                        orderListPost.pay = "1";

                        orderListPost.execute(OrderActivity.this);

                        break;

                    case R.id.order_tab_not_pay_rb:

                        orderListPost.pay = "-1";

                        orderListPost.execute(OrderActivity.this);

                        break;

                }

            }
        });

        //点击事件 写在回调点击方法里了 最下面
//        order_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//                startActivity(new Intent(OrderActivity.this, OrderDetailActivity.class).putExtra("orderId", lists.get(position).id));
//
//            }
//        });


        orderListPost.sid = BaseApplication.BasePreferences.readShopId();
        orderListPost.uid = BaseApplication.BasePreferences.readUserId();
        orderListPost.pay = "";
        orderListPost.time1 = "";
        orderListPost.time2 = "";

        orderListPost.execute(OrderActivity.this, 1);

        setAppCallBack(new RefrehOrderCallback());

        order_time_end_tv.setOnClickListener(this);
        order_time_start_tv.setOnClickListener(this);
        order_search_tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.order_search_tv:

                String entTime = order_time_end_tv.getText().toString();
                String startTime = order_time_start_tv.getText().toString();

                if (TextUtils.isEmpty(startTime)) {

                    MyToast.showToast("请选择开始时间", context);
                    return;

                } else if (TextUtils.isEmpty(entTime)){

                    MyToast.showToast("请选择结束时间", context);
                    return;

                }

                if (Long.valueOf(TimeUtil.dataOne(entTime)) - Long.valueOf(TimeUtil.dataOne(startTime)) < 0) {

                    MyToast.showToast("起始时间 大于 结束时间", context);

                    return;
                }

                orderListPost.time1 = startTime;
                orderListPost.time2 = entTime;

                orderListPost.execute(OrderActivity.this);

                break;

            case R.id.order_time_start_tv:

                DateTimePickDialogUtil dateUtil = new DateTimePickDialogUtil(this, null);
                dateUtil.dateTimePicKDialog(order_time_start_tv);

                break;

            case R.id.order_time_end_tv:

                DateTimePickDialogUtil util = new DateTimePickDialogUtil(this, null);
                util.dateTimePicKDialog(order_time_end_tv);

                break;

        }

    }


    @Override
    public void pay(int pos) {

        Bundle bundle = new Bundle();

        bundle.putString("oid", lists.get(pos).id);

        startActivity(new Intent(this, ConfirmOrderActivity.class).putExtras(bundle));

    }

    @Override
    public void continueChoose(int pos) {

        Bundle bundle = new Bundle();

        bundle.putString("seats", lists.get(pos).seats);
        bundle.putString("oid", lists.get(pos).id);
        bundle.putBoolean("isFromOrder", true);

        startActivity(new Intent(this, HomeActivity.class).putExtras(bundle));

    }

    @Override
    public void notPayOnItemclick(int pos) {

        startActivity(new Intent(OrderActivity.this, OrderDetailActivity.class).putExtra("orderId", lists.get(pos).id));

    }

    private void setLastUpdateTime() {

        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String time = sDateFormat.format(System.currentTimeMillis());

        pullToRefreshListView.setLastUpdatedLabel(time);

    }

    public class RefrehOrderCallback implements AppCallBack {

        public void  refreshOrder(){

            order_time_start_tv.setText("");
            order_time_end_tv.setText("");

            orderListPost.sid = BaseApplication.BasePreferences.readShopId();
            orderListPost.uid = BaseApplication.BasePreferences.readUserId();
            orderListPost.time1 = "";
            orderListPost.time2 = "";
            orderListPost.pay = "";

            orderListPost.execute(OrderActivity.this, false, 2);

            Log.e("---", "orderActivity--->RefrehOrderCallback刷新");
        }

    }

}
